# 练习12.3
import pygame
import sys
from new_ship import Ship
from settings import Settings

class Rocket_game:
    def __init__(self):
        pygame.init()
        pygame.display.set_caption('Rocket Game')
        self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
        self.clock = pygame.time.Clock()
        self.ship = Ship(self)

    def run_game(self):
        while True:
            self._check_events()
            self.ship.update()
            self._update_screen()
            self.clock.tick(60)

    def _check_events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                self._check_keydown_events(event)
            elif event.type == pygame.KEYUP:
                self._check_keyup_events(event)

    def _check_keydown_events(self, event):
        if event.key == pygame.K_RIGHT:
            self.ship.moving_right = True
        elif event.key == pygame.K_LEFT:
            self.ship.moving_left = True
        elif event.key == pygame.K_DOWN:
            self.ship.moving_down = True
        elif event.key == pygame.K_q:
            sys.exit()

    def _check_keyup_events(self, event):
        if event.key == pygame.K_LEFT:
            self.ship.moving_left = False
        if event.key == pygame.K_RIGHT:
            self.ship.moving_right = False
        if event.key == pygame.K_DOWN:
            self.ship.moving_down = False


    def _update_screen(self):
        """更新屏幕上的图片， 并切换到新屏幕"""
        self.ship.blitme()

        pygame.display.flip()

if __name__ == '__main__':
    # 创建游戏实例并运行游戏
    ai = Rocket_game()
    ai.run_game()


